package com.example.xinyun.slice;

import com.example.xinyun.MyApplication;
import com.example.xinyun.ResourceTable;
import com.example.xinyun.domain.UrlContant;
import com.example.xinyun.db.User;
import com.example.xinyun.db.db_User;
import com.example.xinyun.domain.MD5Util;
import com.google.gson.Gson;
import com.zzrv5.mylibrary.ZZRCallBack;
import com.zzrv5.mylibrary.ZZRHttp;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.TextField;
import ohos.agp.window.dialog.ToastDialog;

public class ChangePasswordSlice extends AbilitySlice {
    String passWord;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_change_password);
        TextField rawPassword = (TextField) findComponentById(ResourceTable.Id_rawPassword);
        TextField newPassword = (TextField) findComponentById(ResourceTable.Id_newPassword);
        TextField notarizePassword = (TextField) findComponentById(ResourceTable.Id_notarizePassword);
        Button chagePassword = (Button) findComponentById(ResourceTable.Id_change_password);
        chagePassword.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {

                ZZRHttp.postJson(UrlContant.URL+"selectUser", "{\"user\":\"" + MyApplication.username + "\"}", new ZZRCallBack.CallBackString() {
                    @Override
                    public void onFailure(int i, String s) {
                        new ToastDialog(getContext())
                                .setText("网络连接失败！")
                                .show();
                    }

                    @Override
                    public void onResponse(String s) {
                        db_User db_user = new Gson().fromJson(s, db_User.class);
                        passWord=db_user.getData().getPassword();
                        if (rawPassword.getText().equals("")||newPassword.getText().equals("")||notarizePassword.getText().equals(""))
                        {
                            new ToastDialog(getContext())
                                    .setText("输入不能为空")
                                    .show();
                        }else {
                            if (MD5Util.getMD5Str(rawPassword.getText()).equals(passWord))
                            {
                                if (newPassword.getText().equals(notarizePassword.getText()))
                                {
                                    User user=new User(MyApplication.username,MD5Util.getMD5Str(newPassword.getText()),null);
                                    ZZRHttp.postJson(UrlContant.URL+"updateUser", user.updataPassWord(), new CallBackString() {
                                        @Override
                                        public void onFailure(int i, String s) {
                                            new ToastDialog(getContext())
                                                    .setText("网络连接失败！")
                                                    .show();
                                        }

                                        @Override
                                        public void onResponse(String s) {
                                            Intent intent=new Intent();
                                            //页面跳转
                                            present(new SettingSlice(),intent);
                                        }
                                    });
                                }else {
                                    new ToastDialog(getContext())
                                            .setText("两次输入的密码不同")
                                            .show();
                                }
                            }else{
                                new ToastDialog(getContext())
                                        .setText("原密码输入错误")
                                        .show();
                            }
                        }
                    }
                });
            }
        });


    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
